<?php

namespace App\Services;

use App\Constants\ResponseCodeConstant;
use App\Exceptions\BusinessException;
use App\Models\AdminUser;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class AdminUserService extends BaseService
{
    /**
     * 注册管理员用户
     * @param array $data
     * @return bool
     */
    public function register(array $data): bool
    {
        try {
            AdminUser::query()->create($data);
            return true;
        } catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * 根据用户名查找用户
     * @param string $username
     * @return Builder|Model|object|null
     * @throws BusinessException
     */
    public function getByUsername(string $username)
    {
        try {
            $admin_user = AdminUser::query()->where('username','=',$username)->first();
            if (is_null($admin_user)) {
                throw new BusinessException(ResponseCodeConstant::USER_NOT_EXISTS);
            }
            return $admin_user;
        } catch (\Exception $exception) {
            throw new BusinessException(ResponseCodeConstant::ACCOUNT_FAILED,$exception->getMessage());
        }
    }

    /**
     * 根据用户ID查找用户
     * @param int $userId
     * @return Builder|Builder[]|\Illuminate\Database\Eloquent\Collection|Model
     * @throws BusinessException
     */
    public function getById(int $userId)
    {
        try {
            $admin_user = AdminUser::query()->find($userId);
            if (is_null($admin_user)) {
                throw new BusinessException(ResponseCodeConstant::USER_NOT_EXISTS);
            }
            return $admin_user;
        } catch (\Exception $exception) {
            throw new BusinessException(ResponseCodeConstant::ACCOUNT_FAILED,$exception->getMessage());
        }
    }

    /**
     * 根据用户ID查找用户角色
     * @param int $userId
     * @return Builder|Builder[]|\Illuminate\Database\Eloquent\Collection|Model
     * @throws BusinessException
     */
    public function getUserRolesById(int $userId)
    {
        try {
            $admin_user = AdminUser::query()->with(['roles'])->find($userId);
            if (is_null($admin_user)) {
                throw new BusinessException(ResponseCodeConstant::USER_NOT_EXISTS);
            }
            return $admin_user;
        } catch (\Exception $exception) {
            throw new BusinessException(ResponseCodeConstant::ACCOUNT_FAILED,$exception->getMessage());
        }
    }

    /**
     * 根据用户名或姓名分页获取用户列表
     * @param $keyword
     * @param $pageSize
     * @param $pageNum
     * @return mixed
     */
    public function getList($keyword,$pageSize,$pageNum)
    {
        $query = AdminUser::query();
        if ($keyword) {
            $query = $query->where(function ($query) use ($keyword){
                $query->where('username','like','%'.$keyword.'%')
                    ->OrWhere('nick_name','like','%'.$keyword.'%');
            });
        }
        $columns = ['username','avatar','nick_name','email'];
        return $query->paginate($pageSize, $columns, $pageName = 'page', $pageNum);
    }

    /**
     * 修改指定用户信息
     * @param $data
     * @param $userId
     * @throws BusinessException
     */
    public function updateById($data,$userId)
    {
        try {
            $count = AdminUser::query()->where('id','=',$userId)->update($data);
            if (!$count) {
                throw new BusinessException(ResponseCodeConstant::COMMON_ERROR,'更新失败');
            }
        }catch (\Exception $exception) {
            throw new BusinessException(ResponseCodeConstant::COMMON_ERROR,$exception->getMessage());
        }
    }

    /**
     * 删除指定用户信息
     * @param $userId
     * @throws BusinessException
     */
    public function deleteById($userId)
    {
        try {
            $count = AdminUser::query()->where('id','=',$userId)->delete();
            if (!$count) {
                throw new BusinessException(ResponseCodeConstant::COMMON_ERROR,'删除失败');
            }
        }catch (\Exception $exception) {
            throw new BusinessException(ResponseCodeConstant::COMMON_ERROR,$exception->getMessage());
        }
    }

}
